package net.babeuf.form.fields.combo 
{
	import fl.controls.ComboBox;
	import flash.events.Event;
	import flash.events.FocusEvent;
	/**
	 * ...
	 * @author damien pegoraro
	 */
	public class ComponentComboField extends ComboField
	{
		protected var _combo  : ComboBox ; 
		
		
		public function ComponentComboField( combo  : ComboBox ) 
		{
			_combo = combo ; 
			_combo.addEventListener(Event.CHANGE, _notifyChanged) ; 
			_combo.addEventListener(FocusEvent.FOCUS_IN, _throwFocusIn ) ;
			_combo.addEventListener(FocusEvent.FOCUS_OUT, _throwFocusOut ) ;
			super() ; 			
		}
		
		override public function empty():void 
		{
			_combo.selectedIndex = 0 ; 
			super.empty();
		}
		
		override public function setTabIndex(index:int): int 
		{
			_combo.tabChildren = false ; 
			_combo.tabIndex = index ; 
			return super.setTabIndex(index) ; 
		}
		
		override public function dispose():void 
		{
			super.dispose();
			if ( _combo ) 
			{
				_combo.removeEventListener(Event.CHANGE, _notifyChanged) ; 			
				_combo.removeEventListener(FocusEvent.FOCUS_IN, _throwFocusIn ) ;
				_combo.removeEventListener(FocusEvent.FOCUS_OUT, _throwFocusOut ) ;
			}
			_combo = null ;
		}
		
		
		override public function getValue():* 
		{
			var ret : Object = _combo.selectedItem ; 
			var notEmpty : Boolean = false ;			
			for ( var i : String in ret )  if ( i != "label" ) notEmpty = true ; 
			return notEmpty ? (( _valueProperty && _valueProperty != "" ) ?  _combo.selectedItem[_valueProperty] : _combo.selectedItem)  : null ;	
		}
		
		public function getProperty( name : String ) : *
		{
			return _combo.selectedItem[name] ; 
		}
		
		
		public function getCombo() : ComboBox
		{
			return _combo ;
		}
		
	}

}